package concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @File Info: leetcode -- <ThreeThreadExecuteQueued>
 * @Author: 18362
 * @Create: 2022-03-14 13:38:57 星期一
 */
public class ThreeThreadExecuteQueued {

    static class MyThread extends Thread {

        private CountDownLatch waitLatch;
        private CountDownLatch selfLatch;

        public MyThread(String name, CountDownLatch waitLatch, CountDownLatch selfLatch) {
            super(name);
            this.waitLatch = waitLatch;
            this.selfLatch = selfLatch;
        }

        @Override
        public void run() {
            try {
                waitLatch.await();
                System.out.println(getName() + " start running!");
                Thread.sleep(3000);
                System.out.println(getName() + " over");
                selfLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);
        CountDownLatch latch3 = new CountDownLatch(1);
        new MyThread("thread-1", latch1, latch2).start();
        new MyThread("thread-2", latch2, latch3).start();
        new MyThread("thread-3", latch3, latch3).start();
        latch1.countDown();
    }
}
